﻿using com.guojin.mj.net.message;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using com.guojin.mj.net.message.game;
using System;

    class OnChatItemClick : MonoBehaviour,IPointerClickHandler {
        public string ID = "";
        public int msgType = 0;

    public void OnPointerClick(PointerEventData eventData)
    {
        Debug.Log("发送chat字符串 :" + ID);

        MessageManager.GetInstance().SendMsg1224(ID, msgType);

        Destroy(gameObject.transform.root.FindChild("addNode/GameRoomPage(Clone)/FightAddNode/ChatPage").gameObject);
    }
}

    class ChatSendMessagePage : Observer{
        private Button closeBtn, sendChatTextBtn;
        private Toggle emojiTl, usefulWordTl;
        private GameObject emojiPage, usefulWordPage;
        private Text chatText;

        private void Start() {
            //初始化UI
            InitUI();

            //初始化表情
            InitNormalEmoji();

            //初始化常用语
            InitUsefulWord();

            //初始化消息
            initMsg();
        }

        protected override string[] GetMsgList() {
            return new string[] {
                MessageFactoryImpi.instance.getMessageString(12,24)
            };
        }

        private void OnSwitchToggle(bool isOn)
        {
            emojiPage.SetActive(emojiTl.isOn);
            usefulWordPage.SetActive(usefulWordTl.isOn);

            emojiTl.transform.FindChild("Background").GetComponent<Image>().sprite = emojiTl.isOn ? Resources.Load<Sprite>("BDUI/chat/open1") : Resources.Load<Sprite>("BDUI/chat/close1");
            usefulWordTl.transform.FindChild("Background").GetComponent<Image>().sprite = usefulWordTl.isOn ? Resources.Load<Sprite>("BDUI/chat/open2") : Resources.Load<Sprite>("BDUI/chat/close2");
        }

        private void InitUI() {
            closeBtn = transform.FindChild("blackBg").GetComponent<Button>();
            closeBtn.onClick.AddListener(OnCloseBtnClicked);

           chatText = transform.FindChild("Bg/InputField/chatText").GetComponent<Text>();
           sendChatTextBtn = transform.FindChild("Bg/sendChatTextBtn").GetComponent<Button>();
           sendChatTextBtn.onClick.RemoveAllListeners();
           sendChatTextBtn.onClick.AddListener(sendChatTextBtnClick);

            emojiTl = transform.FindChild("Bg/ToggleGroup/ToggleEmoji").GetComponent<Toggle>();
            usefulWordTl = transform.FindChild("Bg/ToggleGroup/ToggleUsefulWord").GetComponent<Toggle>();
            emojiTl.onValueChanged.AddListener(OnSwitchToggle);
            usefulWordTl.onValueChanged.AddListener(OnSwitchToggle);
            emojiPage = transform.FindChild("Bg/Normal_Emojis").gameObject;
            usefulWordPage = transform.FindChild("Bg/UsefulWord").gameObject;
            emojiPage.SetActive(false);

            emojiTl.transform.FindChild("Background").GetComponent<Image>().sprite = Resources.Load<Sprite>("BDUI/chat/close1");
            usefulWordTl.transform.FindChild("Background").GetComponent<Image>().sprite = Resources.Load<Sprite>("BDUI/chat/open2");
        }

        private void InitNormalEmoji() {
        for (int i = 1; i <= 55; i++)
        {
            string path = "";
            if (i < 10)
            {
                path = "00" + i;
            }
            else
            {
                path = "0" + i;
            }

            GameObject emoji = Instantiate(Resources.Load("SHPrefab/emojiModel")) as GameObject;
            emoji.GetComponent<Image>().sprite = Resources.Load<Sprite>("Emoji/emotion_" + path);
            emoji.transform.SetParent(transform.FindChild("Bg/Normal_Emojis/Viewport/Content"));
            emoji.transform.localPosition = Vector3.zero;
            emoji.transform.localScale = Vector3.one;
            emoji.name = path;
            emoji.AddComponent<OnChatItemClick>().ID = emoji.name;
            emoji.GetComponent<OnChatItemClick>().msgType = 2;
        }
    }

        private void InitUsefulWord() {
            Transform content = usefulWordPage.transform.FindChild("Scroll View/Viewport/Content");
            foreach (Transform item in content) {
            if (GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].sex == 0)
            {
                item.gameObject.AddComponent<OnChatItemClick>().ID = item.name + "_W";
            }
            else
            {
                item.gameObject.AddComponent<OnChatItemClick>().ID = item.name + "_M";
            }
              
                item.gameObject.GetComponent<OnChatItemClick>().msgType = 3;
            }
        }

        private void sendChatTextBtnClick()
        {
            Debug.Log("发送chat字符串 :" + chatText.text);

            if (!string.IsNullOrEmpty(chatText.text))
            {
                   MessageManager.GetInstance().SendMsg1224(chatText.text, 1);
            }       

            Destroy(gameObject.transform.root.FindChild("addNode/GameRoomPage(Clone)/FightAddNode/ChatPage").gameObject);
        }

        private void OnCloseBtnClicked() {
            Destroy(gameObject);
        }
    }
